﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Concrete;
using SimpleTaskManager.WebUI.Infrastructure.Abstract;
using SimpleTaskManager.WebUI.Infrastructure.Concrete;

namespace SimpleTaskManager.WebUI.Infrastructure
{
    /// <summary>
    /// A class which performs dependency injenction on the creation of a controller
    /// </summary>
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        /// <summary>
        /// Creates a new instance of the controller factory, and sets up bindings.
        /// </summary>
        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        /// <summary>
        /// Provides an instance of a chosen controller.
        /// </summary>
        /// <param name="requestContext">The context of the request.</param>
        /// <param name="controllerType">The desired controller type.</param>
        /// <returns>A dependency injected new controller instance.</returns>
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                ? null
                : (IController)ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            ninjectKernel.Bind<IUserRepository>().To<EFUserRepository>();
            ninjectKernel.Bind<IProjectRepository>().To<EFProjectRepository>();
            ninjectKernel.Bind<ISkillRepository>().To<EFSkillRepository>();
            ninjectKernel.Bind<ITaskRepository>().To<EFTaskRepository>();
            ninjectKernel.Bind<IUser_SkillRepository>().To<EFUser_SkillRepository>();
            ninjectKernel.Bind<IWorkingHoursEntryRepository>().To<EFWorkingHoursEntryRepository>();
            ninjectKernel.Bind<IReportRepository>().To<EFReportRepository>();
            ninjectKernel.Bind<IAuthProvider>().To<FormsAuthProvider>();
            ninjectKernel.Bind<IPasswordEncrypter>().To<SHA1PasswordEncrypter>();
        }
    }
}